 
// export * from "./setups"

import { BodySetup } from "./CreepSetup";

const movemap:Record<string,number>=   {
    ZO: 2,
    ZHO2: 3,
    XZHO2:4,
};
// import { BodySetup } from "utils/CreepSetup";
const rangeattackmap:Record<string,number>=   {
    KO: 2,
    KHO2: 3,
    XKHO2:4,
};
const attackmap:Record<string,number>=   {
    UH: 2,
    UH2O: 3,
    XUH2O:4,
};
const healmap:Record<string,number>=   {
    LO: 2,
    LHO2: 3,
    XLHO2:4,
};
export class CreepAbilityHelper
{
    private static offset=[ [0,-1],[1,-1],[1,0],[1,1],[0,1],[-1,1],[-1,0],[-1,-1] ];

	private static tough_ratio:{[key:string]:number}={
		GO:0.7,
		GHO2:0.5,
		XGHO2:0.3,
	}

	static getCreepAttackAbility(creep:Creep,ingore_0hitpart=true):number
	{
		const attackdamage =  BodySetup.GetBodyAbility(ATTACK,"attack","attack",creep.body,ingore_0hitpart);
		return attackdamage;
	}
	static getCreepRangeAttackAbility(creep:Creep,ingore_0hitpart=true):number
	{
		const attackdamage = BodySetup.GetBodyAbility(RANGED_ATTACK,"ranged_attack","rangedAttack",creep.body,ingore_0hitpart);
		return attackdamage;
	}
	static getCreepHealAbility(creep:Creep):number
	{
		const val = BodySetup.GetBodyAbility(HEAL,"heal","heal",creep.body);
		return val;
	}

	/*承受伤害*/
    /**
     * 计算Damge打在爬身上 造成多少点伤害
     * @param creep 
     * @param Damge 
     * @returns 
     */
	static GetRealBearDamage(creep:Creep,Damge:number)
    {
        // 根据爬的body计算真实伤害
		let realdamage=0;
		for(const bodypart of creep.body)
		{
			if(bodypart.type == "tough")
			{
				if(bodypart.boost)
				{
					const radio = this.tough_ratio[bodypart.boost];
					const partdamage = bodypart.hits/radio; // 承受的伤害
					if(partdamage>Damge)
					{
						realdamage+= Damge*radio;
						Damge-=partdamage;	// 真实伤害
					}
					else
					{
						realdamage+=bodypart.hits;
						Damge-=partdamage;	// 真实伤害
					}

					if(Damge<=0)
					{
						break;
					}
				}
			}
		}
        if(Damge>0)
        {
          realdamage+=Damge;
        }

        return realdamage;
    }
	/*承受击穿护甲伤害*/
    /**
     * 计算爬 在 heal的治疗下。需要多少的攻击力才能把护甲击穿
     * @param creep 
     * @param heal 
     * @returns 
     */
	static GetBrokenToughDamage(creep:Creep,heal:number)
    {
        // 根据爬的body计算真实伤害
		let realdamage=0;
		let tmpheal = heal;
		for(const bodypart of creep.body)
		{
			if(bodypart.type == "tough")
			{
				if(bodypart.boost)
				{
					const radio = this.tough_ratio[bodypart.boost];
					if(tmpheal>bodypart.hits)
					{
						realdamage+=bodypart.hits/radio;
						tmpheal-=bodypart.hits;
					}
					else
					{
						realdamage+=tmpheal/radio;
						tmpheal=0;
						break;
					}
				}
			}
		}
        return realdamage+tmpheal;
    }

    /**
     * 根据伤害值，计算最小能够承受住伤害得body配置 tough 和heal
     * @param damage 伤害值
     * @param heal heal化合物
	 * @param tough tough化合物
	 * 
     */
    static GetMinBodyForDamage(damage:number,heal?:string,tough?:string):Record<BodyPartConstant,number>
    {
        const toughradio=tough?this.tough_ratio[tough]:1;
        const healradio=heal?healmap[heal]:1;
        const realdamage = damage*toughradio;
        const toughcount = Math.ceil(realdamage/100);

        const realheal =  toughcount*100;

        const healcount = Math.ceil( realdamage/(12*healradio));
        return {[TOUGH]:toughcount,[HEAL]:healcount} as Record<BodyPartConstant,number>
    }
	static GetMinBodyForDamageOfAttack(damage:number,attack?:string)
	{
		const radio = attack?attackmap[attack]:1;
		return   Math.ceil( damage/ (30*radio))
	}
	static GetMinBodyForDamageOfRangeAttack(damage:number,attack?:string)
	{
		const radio = attack?rangeattackmap[attack]:1;
		return   Math.ceil( damage/ (10*radio))
	}
	static GetMinBodyForMove(bodycount:number,minerial?:string)
	{
		const radio = minerial?movemap[minerial]:1;
		bodycount/radio
		return   Math.ceil( bodycount/radio)
	}
}
